﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNS.Models
{
    public class CanBoModels
    {
        public static List<CanBo> GetDSCanBoOfPhongBan(int idPhongBan)
        {

            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var query = from dt in context.DSCanBo
                        where dt.IDPhongBan == idPhongBan && dt.TrangThai != 2
                        select dt;
            return query.ToList<CanBo>();
        }
        public static List<CanBo> GetDSCanBo()
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSCanBo
                        select cb;
            return query.ToList<CanBo>();
        }
        public static CanBo GetChiTietCanBo(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSCanBo
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }
        public static int Add(CanBo th)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            context.DSCanBo.AddObject(th);
            context.SaveChanges();
            return th.ID;
        }
        public static void Update(CanBo id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSCanBo
                        where h.ID == id.ID
                        select h;
            CanBo kq = query.First<CanBo>();
            kq = id;
            context.DSCanBo.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
        public static void Delete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from h in context.DSCanBo
                         where h.ID == id
                         select h).FirstOrDefault();

            context.DSCanBo.DeleteObject(query);
            context.SaveChanges();

        }
    }
}